package com.zhudoo.zhilian.dao.impl;

import com.zhudoo.zhilian.dao.entity.Chat;
import com.zhudoo.zhilian.dao.entity.ChatRecord;
import com.zhudoo.zhilian.dao.repository.ChatRecordRepository;
import com.zhudoo.zhilian.dao.repository.ChatRepository;
import com.zhudoo.zhilian.util.service.ChatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhudao
 */
@Component
public class ChatDao {

    @Autowired
    private ChatRepository chatRepository;

    @Autowired
    private ChatRecordRepository chatRecordRepository;

    public List<Chat> getChatListByUserId(Integer userId, Integer page, Integer limit) {
        Pageable pageable = PageRequest.of(page - 1, limit, Sort.by("updateTime").descending());
        List<Chat> chatListList = chatRepository.findByUserIdAndValid(userId, pageable, true);
        return chatListList;
    }

    public List<ChatRecord> getChaRecordByUserId(Integer userId, Integer toUserId, Integer tipId, Integer page, Integer limit) {
        Chat chat = chatRepository.findByUserIdAndToUserIdAndTipId(userId, toUserId, tipId);
        if (chat == null) {
            return new ArrayList<>();
        }
        // Pageable pageable = PageRequest.of(page - 1, limit, Sort.by("createTime").ascending());
        List<ChatRecord> chatListList = chatRecordRepository.findBySessionId(chat.getSessionId(), Sort.by("createTime").ascending());
        return chatListList;
    }

    public void saveChatRecord(Integer userId, Integer toUserId,
                               Integer tipId, Integer contentType,
                               String content) {
        Date nowDate = new Date();
        String sessionId;
        //查询是否有创建联系人,需要通过userId和toUserId查询生成的sessionId
        Chat chat = chatRepository.findByUserIdAndToUserIdAndTipId(userId, toUserId, tipId);
        if (chat == null) {
            sessionId = ChatUtil.getSessionId(userId, toUserId, tipId);
            //创建联系人
            String chatContent = ChatUtil.getContent(contentType, content);
            Chat myChat = new Chat();
            myChat.setSessionId(sessionId);
            myChat.setContent(chatContent);
            myChat.setCreateTime(nowDate);
            myChat.setValid(true);
            myChat.setUnreadCount(0);
            myChat.setToUserId(toUserId);
            myChat.setUserId(userId);
            myChat.setTipId(tipId);
            myChat.setUpdateTime(nowDate);
            chatRepository.save(myChat);
            Chat toChat = new Chat();
            toChat.setSessionId(sessionId);
            toChat.setContent(chatContent);
            toChat.setCreateTime(nowDate);
            toChat.setUpdateTime(nowDate);
            toChat.setValid(true);
            toChat.setUnreadCount(1);
            toChat.setToUserId(userId);
            toChat.setUserId(toUserId);
            toChat.setTipId(tipId);
            chatRepository.save(toChat);
        } else {
            sessionId = chat.getSessionId();
            List<Chat> chatList = chatRepository.findBySessionId(chat.getSessionId());
            for (Chat chat1 : chatList) {
                chat1.setValid(true);
                //对方的未读加1
                if (chat1.getUserId().intValue() == toUserId) {
                    chat1.setUnreadCount(chat1.getUnreadCount() + 1);
                }
                chat1.setContent(ChatUtil.getContent(contentType, content));
                chat1.setUpdateTime(nowDate);
                chatRepository.save(chat1);
            }
        }
        //创建聊天内容
        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setCreateTime(nowDate);
        chatRecord.setSessionId(sessionId);
        chatRecord.setUserId(userId);
        chatRecord.setToUserId(toUserId);
        chatRecord.setContent(content);
        chatRecord.setContentType(contentType);
        chatRecord.setValid(true);
        chatRecordRepository.save(chatRecord);
    }

    public void deleteChat(int id, Integer toUserId, Integer tipId) {
        Chat chat = chatRepository.findByUserIdAndToUserIdAndTipId(id, toUserId, tipId);
        chat.setValid(false);
        chatRepository.save(chat);
    }

    public void haveRead(int id, Integer toUserId, Integer tipId) {
        Chat chat = chatRepository.findByUserIdAndToUserIdAndTipId(id, toUserId, tipId);
        chat.setUnreadCount(0);
        chatRepository.save(chat);
    }

    public Long countByTipId(Integer tipId) {
        return chatRepository.countByTipId(tipId);
    }

}
